package com.chaopei.xmljson_parse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author adai
 * @date 2019年5月11日
 */
public class SaxParse {
	public static void go() {// sax解析
		// 1.创建解析工厂对象
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			// 创建SAXParse对象
			SAXParser sax = factory.newSAXParser();
			///// 创建自定义类SAXParseHandler的解析操作对象/////
			SAXParseHandler handler = new SAXParseHandler();
			////路径要注意,绑定要解析的xml文件到解析对象中
			sax.parse("src/books.xml", handler);
			System.out.println("###共有:" + handler.getBookList().size() + "本书###");
			for (Book k : handler.getBookList()) {
				System.out.println(k.getId());
				System.out.println(k.getName());
				System.out.println(k.getAuthor());
				System.out.println(k.getYear());
				System.out.println(k.getPrice());
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}// go
}

// 创建解析工具类
class SAXParseHandler extends DefaultHandler {
	String value = null;
	Book book = null;
	private List<Book> bookList = new ArrayList<>();

	public List<Book> getBookList() {
		return bookList;
	}

	public void setList(List<Book> bookList) {
		this.bookList = bookList;
	}

	// 存储book数
	int bookIndex = 0;

	// (1)重写父类的方法,用于标识解析开始
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		System.out.println("-----------解析开始-----------");
	}

	// (2)重写父类的方法,用于标识解析结束
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		System.out.println("-----------解析结束-----------");
	}

	// (3)真正解析xml
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		// 调用DefaultHandler类的startElement方法
		//qName即qualified name
		super.startElement(uri, localName, qName, attributes);
		//// uri,localName,qName,attributes
		if (qName.equals("book")) {
			// book个数增加
			bookIndex++;
			// 创建book对象
			book = new Book();
			// 开始解析book元素的属性
			System.out.println("==开始遍历一本书的内容==");
			for (int i = 0; i < attributes.getLength(); i++) {
				System.out.print("book的第" + (i + 1) + "个属性名:" + attributes.getQName(i));
				System.out.println(" 属性值:" + attributes.getValue(i));
				if (attributes.getQName(i).equals("id")) {
					book.setId(attributes.getValue(i));
				} // if
			}
		} // if
		//不要它!qName.equals("name")&& 
		else if (!qName.equals("bookstore")) {
			System.out.print("节点名称:" + qName);
		}
	}// start
		// (4)结束某一个book元素解析

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		// 判断是否针对一本书已经遍历结束
		if (qName.equals("book")) {
			bookList.add(book);
			book = null;
			System.out.println("==结束遍历一本书的内容==");
		} else if (qName.equals("name")) {
			book.setName(value);
		} else if (qName.equals("author")) {
			book.setAuthor(value);
		} else if (qName.equals("year")) {
			book.setYear(Integer.valueOf(value));
		} else if (qName.equals("price")) {
			book.setPrice(Double.valueOf(value));
		} // else
	}// endel
		//// 字符数组////

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		//// 获取解析出的字符串数据
		value = new String(ch, start, length);
		if (!value.trim().equals("")) {
			System.out.println("节点值是:" + value.trim());
		}
	}
}